package com.junyu.controller;

import com.junyu.common.Result;
import com.junyu.pojo.dto.ChangeEm;
import com.junyu.pojo.dto.ChangePw;
import com.junyu.pojo.dto.FindPw;
import com.junyu.pojo.dto.LoginParam;
import com.junyu.pojo.vo.LoginLogVo;
import com.junyu.service.LoginService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 登录Controller
 */
@Api(description = "登录Controller")
@RestController
@CrossOrigin
public class LoginController {
    @Autowired
    private LoginService loginService;


    /**
     * 登录接口
     *
     * @param loginParam 登录参数对象
     * @return LoginLogVo 登录日志vo对象
     */
    @ApiOperation(value = "登录接口")
    @PostMapping("/login")
    public Result<LoginLogVo> login(@RequestBody LoginParam loginParam) {
        LoginLogVo loginVo = this.loginService.login(loginParam);
        return Result.ok(loginVo);
    }

    /**
     * 退出登录接口
     *
     * @return Result
     */
    @ApiImplicitParam(name = "token", required = true, paramType = "header", dataType = "String")
    @PostMapping("/logout")
    @ApiOperation(value = "退出登录接口")
    public Result logout() {
        this.loginService.logout();
        return Result.ok();
    }

    /**
     * @param changePw  修改密码参数
     * @return Result
     */
    @ApiImplicitParam(name = "token", required = true, paramType = "header", dataType = "String")
    @ApiOperation(value = "修改密码接口")
    @PutMapping("/change/password")
    public Result changePassword(@RequestBody ChangePw changePw) {
        this.loginService.changePassword(changePw);
        return Result.ok();
    }

    /**
     * @param changeEm 修改邮箱参数
     * @return Result
     */
    @ApiImplicitParam(name = "token", required = true, paramType = "header", dataType = "String")
    @ApiOperation(value = "修改邮箱接口")
    @PutMapping("/change/email")
    public Result changeEmail(@RequestBody ChangeEm changeEm) {
        this.loginService.changeEmail(changeEm);
        return Result.ok();
    }

    /**
     * 记住密码接口
     *
     * @param password 密码
     * @param userName 用户名
     * @return Result
     */
    @ApiOperation(value = "记住密码接口")
    @GetMapping("/remember/password")
    public Result rememberPassword(@RequestParam String password, @RequestParam String userName) {
        this.loginService.rememberPassword(password, userName);
        return Result.ok();
    }

    /**
     * 获取验证码接口
     *
     * @param userName 用户名
     * @param email    邮箱
     * @return
     */
    @ApiOperation(value = "获取验证码接口")
    @GetMapping("/get/code")
    public Result getCode(@RequestParam String userName, @RequestParam String email) {
        this.loginService.getCode(userName, email);
        return Result.ok();
    }

    /**
     * 找回密码接口
     *
     * @param findPw 找回密码参数
     * @return
     */
    @ApiOperation(value = "找回密码接口")
    @PutMapping("/find/password")
    public Result findPassword(@RequestBody FindPw findPw) {
        this.loginService.findPassword(findPw);
        return Result.ok();
    }
}
